<?php

namespace INT\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use INT\AdminBundle\Entity\Seance;
use INT\AdminBundle\Form\SeanceType;

class EmploiController extends Controller {

    public function showEmploiFiliereAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $filiere = $em->getRepository('INTAdminBundle:Filiere')->find($this->getRequest()->get('id'));

        return $this->render('INTAdminBundle:Emploi:calander.html.twig', array('filiere' => $filiere));
    }

    public function editDateSeanceAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getEntityManager();
        $seance = $em->getRepository('INTAdminBundle:seance')->find($id);
        $seance->setJour($request->get('jour'));
        $seance->setHeureDebut($request->get('heure_debut'));
        $seance->setHeureFin($request->get('heure_fin'));
        $em->flush();
        return new \Symfony\Component\HttpFoundation\Response('');
    }

    public function editSeanceFiliereAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getEntityManager();
        $seance = $em->getRepository('INTAdminBundle:seance')->find($id);
        $seance->setSalle($em->getRepository('INTAdminBundle:salle')->find($request->get('salle')));
        $seance->setEnseignant($em->getRepository('INTUserBundle:User')->find($request->get('enseignant')));
        $seance->setMatiere($em->getRepository('INTAdminBundle:matiere')->find($request->get('matiere')));
        $seance->setNumero($request->get('numero'));
        $em->flush();
        return new \Symfony\Component\HttpFoundation\Response($seance->getId());
    }

    public function newSeanceFiliereAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();


        $seance = new Seance();
        $form = $this->createForm(new SeanceType(), $seance);
        if ($request->isMethod('post')) {
            $form->bind($request);
            $seance = $form->getData();
            $seance->setType('cours');
            $em->persist($seance);
            $em->flush();
            return new \Symfony\Component\HttpFoundation\Response($seance->getId());
        }
        return $this->render('INTAdminBundle:Emploi:formSeance.html.twig', array('form' => $form->createView()));
    }

    public function newAction() {
        return $this->render('INTAdminBundle:Emploi:new.html.twig');
    }

    public function getEmploieAction() {
        $id = $this->getRequest()->get('id');
        $filiere = $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Filiere')->find($id);
        return $this->render('INTAdminBundle:Emploi:emploiFiliere.html.twig', array('filiere' => $filiere));
    }

    public function getListChoixAction() {
        $request = $this->getRequest();
        $type = $request->get('type');
        if ($type == 'Filiere') {
            $filieres = $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Filiere')->findAll();
            return $this->render('INTAdminBundle:Emploi:choises.html.twig', array('filieres' => $filieres));
        }
        if ($type == 'Groupe') {
            $choises = $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Groupe')->findAll();
        }
        if ($type == 'Enseignant') {
            $choises = $this->getDoctrine()->getEntityManager()->getRepository('INTUserBundle:User')->findBy(array('type' => 'En'));
        }
        return $this->render('INTAdminBundle:Emploi:choises.html.twig', array('choises' => $choises));
    }

}
